package com.shaoshaohuo.app.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.MyFarmShopAdapter;
import com.shaoshaohuo.app.entity.FarmMyShop;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.CollectivefarmDelatisActivity;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MyFarmShopFragment extends Fragment implements View.OnClickListener, MyFarmShopAdapter.DownUpCallBack, AdapterView.OnItemClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;


    private View view;
    private ListView my_farm_shop_listView;
    private MyFarmShopAdapter my_farm_shop_adapter;
    private List<FarmMyShop.DataBean> data;

    private TextView my_farm_shop_finished_grounding;
    private TextView my_farm_shop_finish_grounding;
    private Boolean finised = true;
    private Boolean finising = true;
    private static final String FINISH_TYPE = "1";
    private static final String FINISHED_TYPE = "2";
    private Mybrocatrecier mybrocatrecier;
    private CollectivefarmDelatisActivity collectivefarmDelatisActivity;

    public MyFarmShopFragment() {
    }

    public static MyFarmShopFragment newInstance(String param1, String param2) {
        MyFarmShopFragment fragment = new MyFarmShopFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_farm_shop, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        bindAdapter();
        RequestPickData(FINISH_TYPE);
        initListener();
    }

    private void bindAdapter() {
        my_farm_shop_listView.setAdapter(my_farm_shop_adapter);
    }

    private void initListener() {
        my_farm_shop_finish_grounding.setOnClickListener(this);
        my_farm_shop_finished_grounding.setOnClickListener(this);
        my_farm_shop_adapter.setDownUpCallBack(this);
        my_farm_shop_listView.setOnItemClickListener(this);
//        collectivefarmDelatisActivity.setShopCallBack(this);

    }

    private void initView() {
        my_farm_shop_listView = (ListView) view.findViewById(R.id.my_farm_shop_listView);
        my_farm_shop_adapter = new MyFarmShopAdapter(getActivity(), data);
        data = new ArrayList<>();
        my_farm_shop_finish_grounding = (TextView) view.findViewById(R.id.my_farm_shop_finish_grounding);
        my_farm_shop_finished_grounding = (TextView) view.findViewById(R.id.my_farm_shop_finished_grounding);
        mybrocatrecier = new Mybrocatrecier();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("farmdata");
        getActivity().registerReceiver(mybrocatrecier, intentFilter);
        collectivefarmDelatisActivity = new CollectivefarmDelatisActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_farm_shop_finish_grounding:
                if (finising) {
                    my_farm_shop_finish_grounding.setTextColor(getResources().getColor(R.color.color_49b152));
                    my_farm_shop_finished_grounding.setTextColor(getResources().getColor(R.color.color_333333));
                    RequestPickData(FINISH_TYPE);//上架商品
                    finising = false;
                    finised = true;
                } else {
                    finising = false;
                    finised = true;
                }
                break;
            case R.id.my_farm_shop_finished_grounding:
                if (finised) {
                    my_farm_shop_finish_grounding.setTextColor(getResources().getColor(R.color.color_333333));
                    my_farm_shop_finished_grounding.setTextColor(getResources().getColor(R.color.color_49b152));
                    RequestPickData(FINISHED_TYPE);//下架商品
                    finising = true;
                    finised = false;
                } else {
                    finising = true;
                    finised = false;
                }
                break;
        }
    }

    private void RequestPickData(String type) {
        ShopHttpConfig.FarmMyShop(getActivity(), type, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
                Log.e("获取商品", "失败");
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        data.clear();
                        my_farm_shop_adapter.refreshAdapter(data);
//                        Toast.makeText(getActivity(), "没有商品", Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                Log.e("获取商品", "成功");
                final FarmMyShop farmShop = (FarmMyShop) o;

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (farmShop.getData() != null) {
                            data = farmShop.getData();
                            my_farm_shop_adapter.refreshAdapter(farmShop.getData());
                        }
                    }
                });
            }
        }, FarmMyShop.class);
    }

    @Override
    public void callBack(String type) {
        RequestPickData(type);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mybrocatrecier != null) {
            getActivity().unregisterReceiver(mybrocatrecier);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), CollectivefarmDelatisActivity.class);
//        intent.putExtra("catid", data.get(position).getCatid());
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", data.get(position));
        if (finising) {
            bundle.putString("type", FINISH_TYPE);
        }
        if (finised) {
            bundle.putString("type", FINISHED_TYPE);
        }
        intent.putExtras(bundle);
        startActivity(intent);
    }

//    @Override
//    public void callBackListener(String type) {
//        RequestPickData(type);
//    }

    class Mybrocatrecier extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("farmdata")) {
                ToastUtil.toast("广播");
                RequestPickData(FINISH_TYPE);
            }
        }
    }
}
